package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.ActivitySupplementSignature;
import com.ruoyi.system.domain.ActivityUserSignature;
import com.ruoyi.system.domain.MeetingSupplementSignature;
import com.ruoyi.system.domain.MeetingUserSignature;
import com.ruoyi.system.mapper.ActivitySupplementSignatureMapper;
import com.ruoyi.system.mapper.ActivityUserSignatureMapper;
import com.ruoyi.system.service.IActivitySupplementSignatureService;
import com.ruoyi.system.service.IActivityUserSignatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 活动补签审批Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-11
 */
@Service
public class ActivitySupplementSignatureServiceImpl implements IActivitySupplementSignatureService
{
    @Autowired
    private ActivitySupplementSignatureMapper activitySupplementSignatureMapper;
    @Autowired
    private IActivityUserSignatureService activityUserSignatureService;
    @Autowired
    private ActivityUserSignatureMapper activityUserSignatureMapper;


    /**
     * 查询活动补签审批
     *
     * @param id 活动补签审批主键
     * @return 活动补签审批
     */
    @Override
    public ActivitySupplementSignature selectActivitySupplementSignatureById(String id)
    {
        return activitySupplementSignatureMapper.selectActivitySupplementSignatureById(id);
    }

    /**
     * 查询活动补签审批列表
     *
     * @param activitySupplementSignature 活动补签审批
     * @return 活动补签审批
     */
    @Override
    public List<ActivitySupplementSignature> selectActivitySupplementSignatureList(ActivitySupplementSignature activitySupplementSignature)
    {
        return activitySupplementSignatureMapper.selectActivitySupplementSignatureList(activitySupplementSignature);
    }

    /**
     * 新增活动补签审批
     *
     * @param activitySupplementSignature 活动补签审批
     * @return 结果
     */
    @Override
    public int insertActivitySupplementSignature(ActivitySupplementSignature activitySupplementSignature)
    {
        String date = DateUtils.dateTimeNow();
        String id = date + IdUtils.fastSimpleEnglishUUID(5);
        activitySupplementSignature.setId(id);
        activitySupplementSignature.setCreateTime(DateUtils.getNowDate());
        return activitySupplementSignatureMapper.insertActivitySupplementSignature(activitySupplementSignature);
    }

    /**
     * 修改活动补签审批
     *
     * @param activitySupplementSignature 活动补签审批
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateActivitySupplementSignature(ActivitySupplementSignature activitySupplementSignature)
    {
        activitySupplementSignature.setUpdateTime(DateUtils.getNowDate());
        int i = activitySupplementSignatureMapper.updateActivitySupplementSignature(activitySupplementSignature);
        ActivitySupplementSignature supplementSignature = activitySupplementSignatureMapper.selectActivitySupplementSignatureById(activitySupplementSignature.getId());
        if(!ObjectUtils.isEmpty(supplementSignature)){
            String status = activitySupplementSignature.getStatus();
            ActivityUserSignature activityUserSignature = new ActivityUserSignature();
            activityUserSignature.setUserId(supplementSignature.getUserId());
            activityUserSignature.setActivityId(supplementSignature.getActivityId());
            ActivityUserSignature signature = activityUserSignatureMapper.selectActivityUserSignature(activityUserSignature);
            if("1".equals( status)){
                //审批通过，修改签到信息为补签
                signature.setApprovalNum(supplementSignature.getId());
                signature.setStatus("4");
                activityUserSignatureService.updateActivityUserSignature(signature);
            }else if("2".equals( status)){
                //（通过之后）审批撤销，修改签到信息为迟到
                signature.setStatus("3");
                activityUserSignatureService.updateActivityUserSignature(signature);
            }
        }
        return i;
    }

    /**
     * 删除活动补签审批信息
     *
     * @param id 活动补签审批主键
     * @return 结果
     */
    @Override
    public int deleteActivitySupplementSignatureById(String id)
    {
        return activitySupplementSignatureMapper.deleteActivitySupplementSignatureById(id);
    }
}
